export * from "./config";
export * from "./request";
import { NOT_PRODUCTION } from "./config";

/**
 * 日志输出
 * @param  {...any} args
 */
export function log(...args) {
  if (NOT_PRODUCTION && window.console) {
    console.log(...args);
  }
}

/**
 * 为数字添加分割符 ,
 * @param value
 * @returns {string}
 */
let numberRegx;
export function fomartNum(value) {
  if (!numberRegx) {
    numberRegx = /(\d)(?=(\d{3})+$)/g;
  }
  const suf = (value || 0).toString().split(".");
  if (suf[0] > 999) {
    suf[0] = suf[0].replace(numberRegx, "$1,");
  }
  return suf[0] + (suf[1] ? "." + suf[1] : "");
}

/**
 * img转换成base64
 * @param {*} file
 * @returns
 */
export function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}
/**
 * 生成guid
 */
export function guid() {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (
    S4() +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    S4() +
    S4()
  );
}

/* 防抖函数 */
export function debounce(func, wait) {
  let delay = wait || 500;
  let timer;
  return function () {
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      timer = null;
      func.apply(this, args);
    }, delay);
  };
}

/**
 * 统一接口返回数据格式
 * @param {*} data
 * @returns
 */
export function unifyCode(data) {
  let da = { ...data };
  da.retcode = data.status;
  da.msg = data.message;
  return da;
}

/**
 * 获取当天时间
 * @param {*} day
 * @returns
 */
export function createCurrentTime() {
  let time = "";
  let d = new Date();
  let year = d.getFullYear();
  let month =
    d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
  let day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
  let h = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
  let m = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
  let s = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
  time = `${year}-${month}-${day} ${h}:${m}:${s}`;
  return time;
}

/**
 * 富文本框数据格式化
 * @param {*} text
 * @returns
 */
export function formateByRegx(text) {
  let reg = new RegExp("<p><br></p>", "g");
  return text.replace(reg, "") || undefined;
}

/**
 * 动态设置font-size
 * @returns
 */
export function setRemSize() {
  const resizeFn = function () {
    const html = document.getElementsByTagName("html")[0];
    const clientWidth = document.documentElement.clientWidth;
    html.style.fontSize = parseInt((clientWidth * 10) / 1920) + "px";
  };
  return resizeFn;
}

/**
 * 下载文件
 * @param {String} fileName
 * @param {String|Blob|ArrayBuffer} content
 */
export function downloadFile(fileName, content) {
  const aLink = document.createElement("a");
  const evt = document.createEvent("HTMLEvents");
  evt.initEvent("click", false, false);
  aLink.download = fileName;
  if (typeof content === "string" && content.indexOf("http") === 0) {
    aLink.href = content;
  } else {
    aLink.href = URL.createObjectURL(new Blob([content]));
  }
  document.body.appendChild(aLink);
  aLink.dispatchEvent(evt);
  if (aLink.click) {
    aLink.click();
  }
  document.body.removeChild(aLink);
  URL.revokeObjectURL(aLink.href);
}
